package nccloud.web.pu.storereq.action;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.pu.m422x.entity.StoreReqAppVO;
import nc.vo.pubapp.query2.sql.process.QuerySchemeProcessor;
import nccloud.dto.pu.storereq.enumeration.TabCodeEnum;
import nccloud.dto.scmpub.page.entity.SCMQueryTreeFormatVO;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.pu.storereq.util.StoreReqScaleUtil;
import nccloud.web.scmpub.page.action.AbstractPageQueryAction;

public class QueryAction extends AbstractPageQueryAction<StoreReqAppVO> {
	public String[] getHeadOrderFields() {
		return new String[]{"vbillcode"};
	}

	public String[] getAllTabs() {
		return TabCodeEnum.AllEnums;
	}

	public String getAllTabCode() {
		return TabCodeEnum.All.value();
	}

	public String getTabConditionClazz() {
		return "nccloud.dto.pu.storereq.rule.StorereqTabCondition";
	}

	protected void modifyQuerySchme(SCMQueryTreeFormatVO queryInfo, IQueryScheme queryScheme) {
		QuerySchemeProcessor processor = new QuerySchemeProcessor(queryScheme);
		// humf二开，密级过滤
		String useridasd = InvocationInfoProxy.getInstance().getUserId();
		String mjsql = " and (vdef10 = '~' or vdef10 in " + " (SELECT pk_defdoc\n"
				+ "FROM bd_defdoc WHERE name IN ( SELECT DISTINCT\n"
				+ "REGEXP_SUBSTR(memo , '[^,]+', 1, LEVEL, 'i') AS test2 FROM\n"
				+ " (SELECT memo FROM bd_defdoc WHERE pk_defdoc IN ( SELECT\n"
				+ "def10 FROM bd_psndoc WHERE pk_psndoc IN ( SELECT pk_psndoc\n" + "FROM sm_user WHERE cuserid = '"
				+ useridasd + "'))) CONNECT BY\n"
				+ "LEVEL <= LENGTH(memo) - LENGTH(REPLACE(memo , ',', '')) + 1) AND pk_defdoclist IN (\n"
				+ "SELECT pk_defdoclist FROM bd_defdoclist WHERE name = '文件定义密级' )))\n";
		processor.appendWhere(mjsql);
		
		//lxiang20231113查待请购
		if(processor.getQueryCondition("iswaitpray") != null) {
			String[] values = processor.getQueryCondition("iswaitpray").getValues();
			if("0".equals(values[0])){
				processor.appendWhere("and pk_storereq in (select DISTINCT pk_storereq from v_streq_qg_111  WHERE state = 'Y')");
			}else if("1".equals(values[0])) {
				processor.appendWhere("and pk_storereq in (select DISTINCT pk_storereq from v_streq_qg_111  WHERE state = 'N')");
			}
		}
		
		//lxiang20231113查是否完结
		
		if(processor.getQueryCondition("isclosed") != null) {  //0未完结   1已完结
			String[] values = processor.getQueryCondition("isclosed").getValues();
			if("0".equals(values[0])){
				processor.appendWhere("and pk_storereq in (select DISTINCT pk_storereq from v_streq_wj_111  WHERE state = 'N')");
			}else if("1".equals(values[0])) {
				processor.appendWhere("and pk_storereq in (select DISTINCT pk_storereq from v_streq_wj_111  WHERE state = 'Y')");
			}
		}
	}
	
	protected void afterProcess(Grid grid) {
		StoreReqScaleUtil stoUtil = new StoreReqScaleUtil();
		stoUtil.processGrid(grid);
	}
}